package school.valon.business.impl;

import org.springframework.transaction.annotation.Transactional;

import school.valon.business.CourseBusiness;
import school.valon.business.base.BaseBusiness;
import school.valon.constantes.EnumCommentType;
import school.valon.dto.CourseDto;
import school.valon.dto.CourseOfferingDto;
import school.valon.dto.CourseOfferingStudentEnrolmentDto;
import school.valon.dto.StudentClassRecordingCommentDto;
import school.valon.dto.StudentClassRecordingDto;
import school.valon.persistence.dao.CourseDao;
import school.valon.persistence.dao.CourseOfferingDao;
import school.valon.persistence.dao.CourseOfferStdEnrolmentDao;
import school.valon.persistence.dao.ParentDao;
import school.valon.persistence.dao.SchoolDao;
import school.valon.persistence.dao.StaffDao;
import school.valon.persistence.dao.StudentClassRecordingDao;
import school.valon.persistence.dao.StudentDao;
import school.valon.persistence.pojo.Course;
import school.valon.persistence.pojo.CourseOffering;
import school.valon.persistence.pojo.CourseOfferingStudentEnrolment;
import school.valon.persistence.pojo.CourseOfferingStudentEnrolmentPK;
import school.valon.persistence.pojo.StudentClassRecording;
import school.valon.persistence.pojo.StudentClassRecordingComment;

@Transactional
public class CourseBusinessImpl extends BaseBusiness implements CourseBusiness {
	private StudentDao studentDao;
	private StaffDao staffDao;
	private ParentDao parentDao;
	private CourseDao courseDao;
	private CourseOfferingDao courseOfferingDao;
	private CourseOfferStdEnrolmentDao courseOfferStdEnrolmentDao;
	private StudentClassRecordingDao studentClassRecordingDao;
	private SchoolDao schoolDao;

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public void setStaffDao(StaffDao staffDao) {
		this.staffDao = staffDao;
	}

	public void setParentDao(ParentDao parentDao) {
		this.parentDao = parentDao;
	}

	public void setCourseDao(CourseDao courseDao) {
		this.courseDao = courseDao;
	}

	public void setCourseOfferingDao(CourseOfferingDao courseOfferingDao) {
		this.courseOfferingDao = courseOfferingDao;
	}

	public void setStudentClassRecordingDao(StudentClassRecordingDao studentClassRecordingDao) {
		this.studentClassRecordingDao = studentClassRecordingDao;
	}

	public void setCourseOfferStdEnrolmentDao(
			CourseOfferStdEnrolmentDao courseOfferStdEnrolmentDao) {
		this.courseOfferStdEnrolmentDao = courseOfferStdEnrolmentDao;
	}

	public void setSchoolDao(SchoolDao schoolDao) {
		this.schoolDao = schoolDao;
	}

	public CourseDto saveCourse(CourseDto courseDto) {
		Course course = null;
		if (courseDto.getCourseId() == null) {
			course = courseDao.create(mapper.map(courseDto, Course.class));
		} else {
			course = courseDao.find(courseDto.getCourseId());
			mapper.map(courseDto, course);
			courseDao.update(course);
		}
		return mapper.map(course, CourseDto.class);
	}

	public void deleteCourse(Integer courseId) {
		courseDao.delete(courseId);
	}

	public CourseOfferingDto saveCourseOffering(CourseOfferingDto courseOfferingDto) {
		CourseOffering courseOffering = null;
		if (courseOfferingDto.getCourseOfferingId() == null) {
			courseOffering = mapper.map(courseOfferingDto, CourseOffering.class);
			if (courseOfferingDto.getCourse() != null && courseOfferingDto.getCourse().getCourseId() != null) {
				courseOffering.setCourse(courseDao.find(courseOfferingDto.getCourse().getCourseId()));
			}
			if (courseOfferingDto.getStaff() != null && courseOfferingDto.getStaff().getStaffId() != null) {
				courseOffering.setStaff(staffDao.find(courseOfferingDto.getStaff().getStaffId()));
			}
			if (courseOfferingDto.getSchool() != null && courseOfferingDto.getSchool().getSchoolId() != null) {
				courseOffering.setSchool(schoolDao.find(courseOfferingDto.getSchool().getSchoolId()));
			}
			courseOffering = courseOfferingDao.create(courseOffering);
		} else {
			courseOffering = courseOfferingDao.find(courseOfferingDto.getCourseOfferingId());
			courseOffering.setStartDate(courseOfferingDto.getStartDate());
			courseOffering.setEndDate(courseOfferingDto.getEndDate());
			courseOfferingDao.update(courseOffering);
		}
		courseOffering.setCourse(null);
		courseOffering.setCourseOfferingStudentEnrolments(null);
		courseOffering.setSchool(null);
		courseOffering.setStaff(null);
		courseOffering.setStudentClassRecordings(null);	

		return mapper.map(courseOffering, CourseOfferingDto.class);
	}

	public void deleteCourseOffering(Integer courseOfferingId) {
		courseOfferingDao.delete(courseOfferingId);
	}

	public StudentClassRecordingDto saveStudentClassRecording(StudentClassRecordingDto studentClassRecordingDto) {
		StudentClassRecording studentClassRecording = null;
		if (studentClassRecordingDto.getStudentClassRecordingId() == null) {
			studentClassRecording = mapper.map(studentClassRecordingDto, StudentClassRecording.class);
			if (studentClassRecordingDto.getCourseOffering() != null && studentClassRecordingDto.getCourseOffering().getCourseOfferingId() != null) {
				studentClassRecording.setCourseOffering(courseOfferingDao.find(studentClassRecordingDto.getCourseOffering().getCourseOfferingId()));
			}
			if (studentClassRecordingDto.getStudent() != null && studentClassRecordingDto.getStudent().getStudentId() != null) {
				studentClassRecording.setStudent(studentDao.find(studentClassRecordingDto.getStudent().getStudentId()));
			}
			studentClassRecording = studentClassRecordingDao.create(studentClassRecording);
		} else {
			studentClassRecording = studentClassRecordingDao.find(studentClassRecordingDto.getStudentClassRecordingId());
			studentClassRecording.setClassDate(studentClassRecordingDto.getClassDate());
			studentClassRecording.setPerformanceMark(studentClassRecordingDto.getPerformanceMark());
			studentClassRecording.setAttendedHours(studentClassRecordingDto.getAttendedHours());
			studentClassRecordingDao.update(studentClassRecording);
		}

		studentClassRecordingDto.setStudentClassRecordingId(studentClassRecording.getStudentClassRecordingId());
		return studentClassRecordingDto;
	}

	public void deleteStudentClassRecording(Integer studentClassRecordingId) {
		studentClassRecordingDao.delete(studentClassRecordingId);
	}

	public StudentClassRecordingCommentDto saveStudentClassRecordingComment(StudentClassRecordingCommentDto studentClassRecordingCommentDto) {
		StudentClassRecordingComment stdClsRecCmt = null;
		if (studentClassRecordingCommentDto.getStudentClassRecordingCommentsId() == null) {
			stdClsRecCmt = mapper.map(studentClassRecordingCommentDto, StudentClassRecordingComment.class);	
			stdClsRecCmt.setCommentType(studentClassRecordingDao.findCommentType(studentClassRecordingCommentDto.getCommentType().getCommentTypeId()));		
			if (EnumCommentType.COMPLAINT_ON_STAFF.getTypeId() == studentClassRecordingCommentDto.getCommentType().getCommentTypeId()) {
				stdClsRecCmt.setParent(parentDao.find(studentClassRecordingCommentDto.getParent().getParentId()));
				stdClsRecCmt.setParentComment(studentClassRecordingCommentDto.getParentComment());
			} else {
				stdClsRecCmt.setStaff(staffDao.find(studentClassRecordingCommentDto.getStaff().getStaffId()));
				stdClsRecCmt.setStaffComment(studentClassRecordingCommentDto.getStaffComment());
			}
			stdClsRecCmt.setStudentClassRecording(studentClassRecordingDao.find(
					studentClassRecordingCommentDto.getStudentClassRecording().getStudentClassRecordingId()));
			stdClsRecCmt =
					studentClassRecordingDao.createStudentClassRecordingComment(stdClsRecCmt);
		} else {
			stdClsRecCmt = studentClassRecordingDao.findStudentClassRecordingComment(
					studentClassRecordingCommentDto.getStudentClassRecordingCommentsId());
			if (EnumCommentType.COMPLAINT_ON_STAFF.getTypeId() == stdClsRecCmt.getCommentType().getCommentTypeId()) {
				stdClsRecCmt.setParentComment(studentClassRecordingCommentDto.getParentComment());
			} else {
				stdClsRecCmt.setStaffComment(studentClassRecordingCommentDto.getStaffComment());
			}
			studentClassRecordingDao.updateStudentClassRecordingComment(stdClsRecCmt);
		}
		studentClassRecordingCommentDto.setStudentClassRecordingCommentsId(stdClsRecCmt.getStudentClassRecordingCommentsId());
		return studentClassRecordingCommentDto;		
	}

	public void deleteStudentClassRecordingComment(Integer studentClassRecordingCommentId) {
		studentClassRecordingDao.deleteStudentClassRecordingComment(
				studentClassRecordingCommentId);
	}

	public CourseOfferingStudentEnrolmentDto saveCourseOfferingStudentEnrolment(CourseOfferingStudentEnrolmentDto courseOffStdEnrolmentDto) {
		CourseOfferingStudentEnrolment courseOffStdEnrolment = null;
		courseOffStdEnrolment = mapper.map(courseOffStdEnrolmentDto, CourseOfferingStudentEnrolment.class);
		courseOffStdEnrolment.setCourseOffering(courseOfferingDao.find(courseOffStdEnrolmentDto.getCourseOffering().getCourseOfferingId()));
		courseOffStdEnrolment.setStudent(studentDao.find(courseOffStdEnrolmentDto.getStudent().getStudentId()));
		courseOffStdEnrolment.setId(new CourseOfferingStudentEnrolmentPK());
		courseOffStdEnrolment.getId().setCourseOfferingId(courseOffStdEnrolmentDto.getCourseOffering().getCourseOfferingId());
		courseOffStdEnrolment.getId().setStudentId(courseOffStdEnrolmentDto.getStudent().getStudentId());
		courseOffStdEnrolment =
				courseOfferStdEnrolmentDao.create(courseOffStdEnrolment);
		courseOffStdEnrolment.setStudent(null);
		courseOffStdEnrolment.setCourseOffering(null);
		return mapper.map(courseOffStdEnrolment, CourseOfferingStudentEnrolmentDto.class);	
	}

	public void deleteCourseOfferingStudentEnrolment(Integer courseOfferingId, Integer studentId) {
		CourseOfferingStudentEnrolmentPK courseOffStdEnrolmentPk = new CourseOfferingStudentEnrolmentPK();
		courseOffStdEnrolmentPk.setCourseOfferingId(courseOfferingId);
		courseOffStdEnrolmentPk.setStudentId(studentId);
		courseOfferStdEnrolmentDao.delete(courseOffStdEnrolmentPk);
	}
}
